<?php
namespace app\admin\controller;

use app\common\model\User as UserModel;
use app\common\model\Role as RoleModel;
/**
 * 后台用户控制器
 */
class User extends Admin 
{
	/**
	 * 用户列表
	 */
	public function index(){
		$key = $this->request->param('key/s');
		$status = $this->request->param('status/d');		
		
		$where = [];
		$map = [];
		$query = [];
		if(!empty($keyword)){
			$where[] = ['username|nickname','like', '%'.$key.'%'];
			$query['key'] = $key;
		}

		if(!empty($status)){
			$map['status'] = $status;
			$query['status'] = $status;
		}

		$list = UserModel::where($map)->where($where)->order('id desc')->paginate(15, false, ['query' => $query]);
		$this->assign('list', $list);

		return $this->fetch();
	}

	/**
	 * 新增用户
	 */
	public function add(){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'username|用户名' => 'require|alphaNum|unique:user',
        		'nickname|昵称'  => 'require',
        		'role|角色'      => 'require',
        		'email|邮箱'     => 'email',
        		'password|密码'  => 'require|length:6,20',
        		'mobile|手机号'   => 'regex:^1\d{10}',
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if($query = UserModel::create($data)){
				$this->success('新增成功');
			}else{
				$this->error('新增失败');
			}
		}else{
			$role = RoleModel::where('status',1)->select();

			$this->assign('title','新增用户');
			$this->assign('role', $role);
			$this->assign('info', null);

			return $this->fetch('edit');
		}
	}

	/**
	 * 编辑用户
	 * @param  integer $id 用户ID
	 * @return mixed
	 */
	public function edit($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			//验证
			$result = $this->validate($data,[
				'email|邮箱'     => 'email',
				'password|密码'  => 'length:6,20',
				'role|角色'      => 'require',
				'mobile|手机号'   => 'regex:^1\d{10}',
			]);
			// 验证失败 输出错误信息
			if(true !== $result) $this->error($result);

			if(empty($data['password'])){
				unset($data['password']);
			}

			if($query = UserModel::update($data)){
				$this->success('编辑成功');
			}else{
				$this->error('编辑失败');
			}
		}else{
			empty($id) && $this->error('参数错误');

			$info = UserModel::where('id',$id)->find();
			$role = RoleModel::where('status',1)->select();

			$this->assign('title','编辑用户');
			$this->assign('info', $info);
			$this->assign('role', $role);

			return $this->fetch();
		}
	}

	/**
	 * 更新状态
	 */
	public function update(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}

		if($query = UserModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 删除会员
	 * @param  integer $id 会员ID
	 * @return mixed
	 */
	public function del($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map['id'] = ['in', $id];
		}elseif(is_numeric($id)){
			$map['id'] = $id;
		}

		if($query = UserModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 修改密码
	 * @return mixed
	 */
	public function updatePassword(){
		if($this->request->isPost()){
			$data = $this->request->post();
			if(empty($data['password'])){
				unset($data['password']);
			}	

			if($query = UserModel::update($data)){
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}else{
			$info = UserModel::where('id', UID)->find();

			$this->assign('info', $info);

			return $this->fetch('updatePassword');
		}
	}

	/**
	 * 角色列表
	 */
	public function role(){
		$name = $this->request->param('name');
		$status = $this->request->param('status');

		$map = [];
		$query = [];
		if(!empty($name)){
			$map['name'] = ['like', '%'.$name.'%'];
			$query['name'] = $name;
		}

		if(!empty($status)){
			$map['status'] = $status;
			$query['status'] = $status;
		}

		$list = RoleModel::where($map)->order('id,sort')->paginate(10, false, ['query' => $query]);

		$this->assign('list', $list);
		return $this->fetch();
	}

	/**
	 * 添加角色
	 */
	public function addRole(){
		if($this->request->isPost()){
			$data = $this->request->post();
			// 验证
            $result = $this->validate($data, [
            	'name|角色名称' => 'require|unique:role',
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if($query = RoleModel::create($data)){
            	$this->success('新增成功');
            }else{
            	$this->error('新增失败');
            }
		}else{
			$this->assign('title','新增角色');
			$this->assign('info',null);
			return $this->fetch('editRole');
		}
	}

	/**
	 * 编辑角色
	 * @param  integer $id 角色ID
	 * @return mixed
	 */
	public function editRole($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();
			// 验证
            $result = $this->validate($data, [
            	'name|角色名称' => 'require|unique:role',
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if($query = RoleModel::update($data)){
            	$this->success('编辑成功');
            }else{
            	$this->error('编辑失败');
            }
		}else{
			empty($id) && $this->error('参数错误');

			$info = RoleModel::where('id',$id)->find();
			$this->assign('title','编辑角色');
			$this->assign('info',$info);

			return $this->fetch('editRole');
		}
	}

	/**
	 * 删除角色
	 * @param  integer $id 角色ID
	 * @return mixed
	 */
	public function delRole($id = 0){
		empty($id) && $this->error('参数错误');

		if(is_array($id)){
			$map['id'] = array('in', $id);
		}elseif(is_numeric($id)){
			$map['id'] = $id;
		}

		if($query = RoleModel::where($map)->delete()){
			$this->success('删除成功');
		}else{
			$this->error('删除失败');
		}
	}

	/**
	 * 更新状态
	 * @return mixed
	 */
	public function updateRole(){
		$data = $this->request->param();

		if(empty($data)){
			$this->error('参数错误');
		}

		if($result = RoleModel::update($data)){
			$this->success('更新成功');
		}else{
			$this->error('更新失败');
		}
	}

	/**
	 * 角色权限
	 * @param  integer $id 角色ID
	 * @return mixed
	 */
	public function auth($id = 0){
		if($this->request->isPost()){
			$data = $this->request->post();

			if (!isset($data['menu_auth'])) {
                $data['menu_auth'] = [];
            }

            if($res = RoleModel::update($data)){
            	$this->success('权限设置成功');
            }else{
            	$this->error('权限设置失败');
            }
		}else{
			empty($id) && $this->error('参数错误');

			$info = RoleModel::where('id',$id)->find();
			$this->assign('info',$info);

			return $this->fetch();
		}
	}
}